"""Build DAOS I/O Engine"""


def scons():
    """Execute build"""
    Import('env', 'prereqs', 'libdaos_tgts')

    if not prereqs.server_requested():
        return

    denv = env.Clone()

    denv.AppendUnique(CPPPATH=[Dir('..').srcnode()])
    denv.Append(CPPDEFINES=['-DDAOS_PMEM_BUILD'])
    libraries = ['daos_common_pmem', 'gurt', 'cart', 'vos_srv']
    libraries += ['bio', 'dl', 'uuid', 'pthread', 'abt']
    libraries += ['hwloc', 'pmemobj', 'protobuf-c', 'isal', 'numa']

    denv.require('argobots', 'protobufc', 'pmdk', 'isal')

    # pylint: disable-next=wrong-spelling-in-comment
    # the "-rdynamic" is to allow other dll to refer symbol defined in
    # daos_engine such as dss_tls_key etc.
    denv.AppendUnique(LINKFLAGS=['-rdynamic'])

    # Add runtime paths for daos libraries
    denv.AppendUnique(RPATH_FULL=['$PREFIX/lib64/daos_srv'])

    sources = ['drpc_client.c', 'drpc_ras.c',
               'drpc_handler.c', 'drpc_listener.c',
               'drpc_progress.c', 'init.c', 'module.c',
               'srv_cli.c', 'profile.c', 'rpc.c',
               'server_iv.c', 'srv.c', 'srv.pb-c.c',
               'sched.c', 'ult.c', 'util.c', 'event.pb-c.c',
               'srv_metrics.c'] + libdaos_tgts

    # Generate I/O Engine program
    engine = denv.d_program('daos_engine', sources, LIBS=libraries)
    denv.Install('$PREFIX/bin', engine)

    if prereqs.test_requested():
        SConscript('tests/SConscript', exports='denv')


if __name__ == "SCons.Script":
    scons()
